package com.ningning.eurkaclient.web;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.EurekaClient;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yangningning
 */
@RestController
public class HelloController {
    private final Logger logger= Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient discoveryClient;

    @Qualifier("eurekaClient")
    @Autowired
    private EurekaClient eurekaClient;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){
        InstanceInfo instanceInfo=eurekaClient.getNextServerFromEureka("HELLO-SERVICE",true);
        instanceInfo.getAppName();
        String homePageUrl=instanceInfo.getHomePageUrl();
        List<ServiceInstance> instanceList=discoveryClient.getInstances("HELLO-SERVICE");
        String serviceId="";
        if (instanceInfo != null && instanceList.size() > 0 ) {
             serviceId=instanceList.get(0).getServiceId();
        }
        return "hello word home page url is"+homePageUrl+"\n\r serviceId:"+serviceId;
    }

    @RequestMapping(value = "/shoutDown",method = RequestMethod.GET)
    public String shutdown(){

        DiscoveryManager.getInstance().shutdownComponent();
        return "shutDown";
    }
}
